<?php
namespace app\index\controller;
use think\Controller;
use think\Log;
use think\Db;
use think\Config;
use think\Url;
use think\Request;
class User extends Base
{

    public function _initialize() {
        $this->checkLogin();
    }

    public function index()
    {


        $whereFriend['f.status']  = 1;
        $whereFriend['f.user_id'] = $this->user['id'];
        $myFriend = Db::name('user_friend')->alias('f')->field('u.id,u.nickname,u.phone,u.sex,u.old,u.pic')
            ->join('__USER__ u','u.id = f.friend_id')
            ->where($whereFriend)->select();
        $this->assign('myFriend',$myFriend);


        $whereFile['status']  = 1;
        $whereFile['user_id'] = $this->user['id'];
        $myFile = Db::name('file_manage')->where($whereFile)->select();
        $this->assign('myFile',$myFile);

        $myInfo = Db::name('user')->where('id',$this->user['id'])->field('id,nickname,phone,sex,old,pic')->find();
        $this->assign('myInfo',$myInfo);

        return $this->fetch();
    }

    /*
     * 添加文件
     */
    public function add()
    {

        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {
                //获取表单上传文件
                $file = request()->file("files");
                if (!isset($file)) {
                    $this->error('请选择上传文件');
                }

                $user_no = $this->user['id'];
                $daytime = date("Ymd");
                //移动到框架应用根目录/public/uploads/ 目录下
                $path = ROOT_PATH . 'public/uploads/' . $user_no . '/' . $daytime;


                if (!is_dir($path)) {
                    mkdir($path, 0777, true);
                }

                $fileinfo = $file->getInfo();
              
                $upload_file = iconv("UTF-8", "GB2312", $fileinfo["name"]);


                $info = $file->move($path, $upload_file,'0');

                if ($info) {

                    // $fileName = $info->getFilename();
                    $addFile['title']    = $fileinfo["name"];
                    $addFile['fileurl']  = $path.'/'.$fileinfo["name"];
                    $addFile['user_id']  = $user_no;
                    $addFile['share']    = 0;
                    $addFile['status']   = 1;
                    $addFile['ctime']    = date("Y-m-d H:i:s");
                    $newId = Db::name('file_manage')->insertGetId($addFile);

                    if($newId){
                        $this->success('文件上传成功','index');
                    }else{
                        $this->error();
                    }


                } else {
                    //上传失败获取错误信息
                    $this->error($file->getError());
                }
                break;
            }
            case "GET": {
                return $this->fetch();
                break;
            }
            default: {
                $this->error('请求方式错误');
                break;
            }
        }

    }



    /*
     * 好友文件分享列表接口
     * post fid:好友id
     */
    public function userfile()
    {

        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $friendId = $_POST['fid'];
                if (intval($friendId)) {
                    $whereFile['status']  = 1;
                    $whereFile['share']   = 1;
                    $whereFile['user_id'] = $friendId;
                    $friendFile = Db::name('file_manage')->where($whereFile)->select();
                    return $this->buildResponse('000000', '操作成功',$friendFile);
                }else{
                    return $this->buildResponse('999999', '参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999', '请求方式错误');
                break;
            }
        }
    }

    /*
 * 回收站列表接口
 * post uid:用户id
 */
    public function recycle()
    {

        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $user_id = $_POST['uid'];
                if (intval($user_id)) {
                    $whereFile['status']  = 0;
                    $whereFile['user_id'] = $user_id;
                    $recycleFile = Db::name('file_manage')->where($whereFile)->select();
                    return $this->buildResponse('000000', '操作成功',$recycleFile);
                }else{
                    return $this->buildResponse('999999', '参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999', '请求方式错误');
                break;
            }
        }
    }


    /*
     * 标题编辑接口
     * post tid:文件id
     *      title:文件名
     */
    public function titleedit(){
        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $fileId = $_POST['tid'];
                $title  = $_POST['title'];
                if(intval($fileId) && $title){

                    $where['id']      = $fileId;
                    $where['user_id'] = $this->user['id'];
                    $checkData = Db::name('file_manage')->where($where)->find();
                    if(!empty($checkData)){
                        if($checkData['status'] == 2){
                            return $this->buildResponse('100000','此文件已删除');
                        }elseif($checkData['status'] == 0) {
                            return $this->buildResponse('200000','请恢复文件后再修改');
                        }else{
                            $update['id']    = $fileId;
                            $update['title'] = $title;
                            Db::name('file_manage')->update($update);
                            return $this->buildResponse('000000','修改成功');
                        }
                    }else{
                        return $this->buildResponse('300000','文件只允许上传者修改');
                    }
                }else{
                    return $this->buildResponse('999999','参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999','请求方式错误');
                break;
            }
        }

    }

    /*
    * 分享接口
    * post tid:文件id
    *      share: 0取消分享 1分享
    */
    public function sharefile(){
        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $fileId = $_POST['tid'];
                $share = $_POST['share'];
                if(intval($fileId)){

                    $where['id']      = $fileId;
                    $where['user_id'] = $this->user['id'];
                    $checkData = Db::name('file_manage')->where($where)->find();
                    if(!empty($checkData)){
                        if($checkData['status'] == 0){
                            return $this->buildResponse('100000','请恢复文件再操作');
                        }elseif($checkData['status'] == 2){
                            return $this->buildResponse('100000','此文件已删除');
                        }elseif($checkData['share'] == 0 && $share == 1) {
                            return $this->buildResponse('200000','此文件已分享');
                        }elseif($checkData['share'] == 1 && $share == 0) {
                            return $this->buildResponse('200000','此文件已取消分享');
                        }else{
                            $update['id']    = $fileId;
                            $update['share'] = intval($share);
                            Db::name('file_manage')->update($update);
                            return $this->buildResponse('000000','操作成功');
                        }
                    }else{
                        return $this->buildResponse('300000','文件只允许上传者修改');
                    }
                }else{
                    return $this->buildResponse('999999','参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999','请求方式错误');
                break;
            }
        }

    }


    /*
     * 回收站还原
     * post tid:文件id
     */
    public function filerestore(){
        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $fileId = $_POST['tid'];
                if(intval($fileId)){
                    $where['id']      = $fileId;
                    $where['user_id'] = $this->user['id'];
                    $checkFile = Db::name('file_manage')->where($where)->value('id');
                    if($checkFile){
                        $update['id']    = $fileId;
                        $update['status'] = 1;
                        Db::name('file_manage')->update($update);
                        return $this->buildResponse('000000','还原成功');
                    }else{
                        return $this->buildResponse('300000','文件只允许上传者还原');
                    }

                }else{
                    return $this->buildResponse('999999','参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999','请求方式错误');
                break;
            }
        }

    }
    /*
     * 移除至回收站
     * post ids: 文件id(用逗号连接)
     */
    public function filesdelete(){
        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $fileId = $_POST['ids'];
                $ids = explode(',',$fileId);
                if(!empty($ids) && count($ids)>0){
                    foreach($ids as $k=> $v){
                        $where['id']      = $v;
                        $where['user_id'] = $this->user['id'];
                        $checkFile = Db::name('file_manage')->where($where)->value('id');
                        if($checkFile){
                            $update['id']    = $v;
                            $update['status'] = 0;
                            Db::name('file_manage')->update($update);
                         }
                    }
                    return $this->buildResponse('000000','删除成功');
                }else{
                    return $this->buildResponse('999999','参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999','请求方式错误');
                break;
            }
        }

    }


    /*
     * 从回收站彻底删除
     * post ids: 文件id(用逗号连接)
     */
    public function filesremove(){
        switch ($_SERVER['REQUEST_METHOD']) {
            case "POST": {

                $fileId = $_POST['ids'];
                $ids    = explode(',',$fileId);
                if(!empty($ids) && count($ids)>0){

                    foreach($ids as $k=> $v){
                        $where['id']      = $v;
                        $where['user_id'] = $this->user['id'];
                        $checkFile = Db::name('file_manage')->where($where)->find();
                        if($checkFile['id']){

                            $update['id']    = $v;
                            $update['status'] = 2;
                            Db::name('file_manage')->update($update);
                            unlink($checkFile['fileurl']);
                        }
                    }
                    return $this->buildResponse('000000','删除成功');
                }else{
                    return $this->buildResponse('999999','参数错误');
                }

                break;
            }
            default: {
                return $this->buildResponse('999999','请求方式错误');
                break;
            }
        }

    }

}
